package com.techsoul.module.pms.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.lang.StringUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.pms.pojo.param.PmsCustomerSaveParam;
import com.techsoul.module.pms.pojo.param.PmsCustomerSearchParam;
import com.techsoul.module.pms.service.IPmsCustomerService;
import com.techsoul.module.pms.service.ITbWmsMaterialReceiptLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 客户 Controller
 *
 * @author liuen
 * @date 2020-12-28
 */
@Api(tags = "客户")
@RestController
@RequestMapping("/pms/pmsCustomer")
public class PmsCustomerController {

    @Autowired
    private Result result;
    @Autowired
    private ITbWmsMaterialReceiptLineService iTbWmsMaterialReceiptLineService;
    @Autowired
    private IPmsCustomerService service;

    @Autowired
    private TokenService tokenService;
    @ApiOperation("新增")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:insert')")
    @PostMapping
    public Result insert(@Valid @RequestBody PmsCustomerSaveParam params) {
        return result.success(this.service.insert(params));
    }
    @ApiOperation("批量生成入库结算")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:generateArForWarehouse')")
    @DeleteMapping("/generateArForWarehouse")
    public Result generateArForWarehouse(@Valid @RequestBody PmsCustomerSaveParam params) {
        if(!StringUtils.isNull(params.getId())){
            return result.success(this.iTbWmsMaterialReceiptLineService.generateArForWarehouse(params.getId(),params.getGenMonth()));

        }else{
            return result.success(this.iTbWmsMaterialReceiptLineService.generateArForWarehouse(params.getGenMonth()));

        }
     }
    @ApiOperation("批量删除（多个用逗号隔开）")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:delete')")
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:update')")
    @PutMapping
    public Result update(@Valid @RequestBody PmsCustomerSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("详情")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:search', 'oms:omsInspectionOrder:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("详情-根据员工ID查询")
    @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:search', 'oms:omsInspectionOrder:search')")
    @GetMapping("/load/{employeeId}")
    public Result findByEmployeeId(@PathVariable("employeeId") String employeeId) {
        return result.success(this.service.findByEmployeeId(employeeId));
    }

    @ApiOperation("列表")
   // @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody PmsCustomerSearchParam params) {
      //  UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
       // params.setSite(sysuser.getSite());

        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-APP所需格式")
   // @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:search', 'oms:omsInspectionOrder:search')")
    @PostMapping("/search/forApp")
    public Result findAllForApp() {
        return result.success(this.service.findAllForApp());
    }

    @ApiOperation("列表-分页")
   // @PreAuthorize("hasAnyAuthority('pms:pmsCustomer:search', 'oms:omsInspectionOrder:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody PmsCustomerSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        params.setSite(sysUser.getSite());

        return result.success(this.service.findAll(params, current, pageSize));
    }

}
